package com.ramblingcoder.bc2.server.tool.update.objects;

import java.io.IOException;
import java.net.Socket;
import java.util.Arrays;
import java.util.Comparator;

import com.ramblingcoder.bc2.server.tool.update.commands.Parcel;
import com.ramblingcoder.bc2.server.tool.update.commands.SupportingFunctions;
import com.ramblingcoder.bc2.server.tool.update.exceptions.InvalidArguments;
import com.ramblingcoder.bc2.server.tool.update.exceptions.LogInRequired;
import com.ramblingcoder.bc2.server.tool.update.exceptions.PlayerAlreadyInList;
import com.ramblingcoder.bc2.server.tool.update.exceptions.PlayerNotInList;

public class Reserved {
	//Variables-----\
	private String name = null;
	//Variables-----/
	
	//Processes-----\	
	public static Reserved[] listReserved(Socket socket) throws IOException, InvalidArguments, LogInRequired {
		SupportingFunctions.sendCommand(socket.getOutputStream(), new String[]{"reservedSlots.list"});
		Parcel response = SupportingFunctions.getResponse(socket.getInputStream())[0];
		
		if (response.getStatus().equals("OK")){
			Reserved[] reserved = Reserved.parseReserved(response);
			
			Arrays.sort(reserved, new Comparator<Reserved>() {
				public int compare(Reserved a, Reserved b) {
					return a.getName().compareTo(b.getName());
				}
			});
			
			return reserved;
		}
		else if (response.getStatus().equals("InvalidArguments"))
			throw new InvalidArguments();
		else if (response.getStatus().equals("LogInRequired"))
			throw new LogInRequired();
		
		return null;
	}
	
	private static Reserved[] parseReserved(Parcel responses) {
		String[] response = responses.getWords();
		
		Reserved[] reserved = new Reserved[response.length - 1];
		
		for (int i = 1; i < response.length; i++){
			String name = response[i];
			
			Reserved reservedSingle = new Reserved();
			reservedSingle.setName(name);
			
			reserved[i-1] = reservedSingle;
		}
		
		return reserved;
	}

	public static boolean remove(Socket socket, Reserved reserved) throws IOException, PlayerNotInList, LogInRequired, InvalidArguments {
		SupportingFunctions.sendCommand(socket.getOutputStream(), new String[]{"reservedSlots.removePlayer", reserved.getName()});
		Parcel response = SupportingFunctions.getResponse(socket.getInputStream())[0];
		
		if (response.getStatus().equals("OK"))
			return true;
		else if (response.getStatus().equals("InvalidArguments"))
			throw new InvalidArguments();
		else if (response.getStatus().equals("LogInRequired"))
			throw new LogInRequired();
		else if (response.getStatus().equals("PlayerNotInList"))
			throw new PlayerNotInList();
		
			
		return false;
	}

	public static boolean add(Socket socket, String name) throws IOException, InvalidArguments, LogInRequired, PlayerAlreadyInList {
		SupportingFunctions.sendCommand(socket.getOutputStream(), new String[]{"reservedSlots.addPlayer", name});
		Parcel response = SupportingFunctions.getResponse(socket.getInputStream())[0];
		
		if (response.getStatus().equals("OK"))
			return true;
		else if (response.getStatus().equals("InvalidArguments"))
			throw new InvalidArguments();
		else if (response.getStatus().equals("LogInRequired"))
			throw new LogInRequired();
		else if (response.getStatus().equals("PlayerAlreadyInList"))
			throw new PlayerAlreadyInList();
			
		return false;
	}
	//Processes-----/

	//Getters/Setters-----\
	public void setName(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	//Getters/Setters-----/
}
